package com.pospi.pai.yunpos.caigou;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.lany.sp.SPHelper;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.pospi.callbacklistener.HttpCallBackListener;
import com.pospi.http.HttpConnection;
import com.pospi.pai.yunpos.R;
import com.pospi.pai.yunpos.base.Base2Activity;
import com.pospi.pai.yunpos.been.SaveOrderInfo;
import com.pospi.pai.yunpos.been.SupBeen;
import com.pospi.pai.yunpos.login.Constant;
import com.pospi.pai.yunpos.report.GoodsReportActivity;
import com.pospi.pai.yunpos.report.GoodsReportSearchActivity;
import com.pospi.pai.yunpos.report.InventorySearchActivity;
import com.pospi.pai.yunpos.report.InventorySearchDetailActivity;
import com.pospi.pai.yunpos.xs.*;


import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SupActivity extends Base2Activity {
    @Bind(R.id.sup_list)
    ListView sup_list;
    List<SupBeen> supBeens = new ArrayList<>();
    private MyAdapter adapter;
    private String mark;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_sup);
        ButterKnife.bind(this);
        setTitle("供应商查询");
        mark = getIntent().getStringExtra("mark");//标记
        adapter = new MyAdapter();
        sup_list.setAdapter(adapter);
        sup_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                switch (mark) {
                    case "CgddActivity":
                        Intent intentCgdd = new Intent(SupActivity.this, CgshListActivity.class);
                        intentCgdd.putExtra("sup", supBeens.get(i));
                        startActivity(intentCgdd);
                        break;
                    case "CgDetailActivity":
                        Intent intentCgDetail = new Intent(SupActivity.this, CgDetailActivity.class);
                        intentCgDetail.putExtra("sup", supBeens.get(i));
                        startActivity(intentCgDetail);
                        break;
                    case "ReceiveDetailActivity":
                        Intent intentReceive = new Intent(SupActivity.this, ReceiveDetailActivity.class);
                        intentReceive.putExtra("sup", supBeens.get(i));
                        startActivity(intentReceive);
                        break;
                    case "SearchReceiveBillActivity":
                        Intent intentSearchReceive = new Intent(SupActivity.this, ReceiveBillListActivity.class);
                        intentSearchReceive.putExtra("sup", supBeens.get(i));
                        startActivity(intentSearchReceive);
                        break;
                    case "RefundDetailActivity":
                        Intent intentRefundDetaile = new Intent(SupActivity.this, RefundDetailActivity.class);
                        intentRefundDetaile.putExtra("sup", supBeens.get(i));
                        startActivity(intentRefundDetaile);
                        break;
                    case "SearchRefundBillActivity":
                        Intent intentSearchRefund = new Intent(SupActivity.this, com.pospi.pai.yunpos.caigou.RefundBillListActivity.class);
                        intentSearchRefund.putExtra("sup", supBeens.get(i));
                        startActivity(intentSearchRefund);
                        break;
                    case "InventorySearchDetailActivity":
                        Intent Inventory = new Intent(SupActivity.this, InventorySearchActivity.class);
                        Inventory.putExtra("sup", supBeens.get(i));
                        startActivity(Inventory);
                        break;
                    case "GoodsReportSearchActivity":
                        Intent GoodsReport = new Intent(SupActivity.this, GoodsReportActivity.class);
                        GoodsReport.putExtra("sup", supBeens.get(i));
                        startActivity(GoodsReport);
                        break;
                    case "OverFlowListActivity":
                        Intent over = new Intent(SupActivity.this, OverFlowListActivity.class);
                        over.putExtra("sup", supBeens.get(i));
                        startActivity(over);
                        break;
                    case "CountsListActivity":
                        Intent Count = new Intent(SupActivity.this, CountsListActivity.class);
                        Count.putExtra("sup", supBeens.get(i));
                        startActivity(Count);
                        break;
                }

                finish();
            }
        });

    }
    @OnClick({R.id.iv_search_xz})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_search_xz:
                downSup();
                break;
        }
    }

    //下载供应商
    private void downSup() {
        Map<String,String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "base.mselect");
        params.put("fun", "getsupplier");
        Log.i("goods", params.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("login", Response);
                try {
                    JSONObject jsonObject = new JSONObject(Response);
                    Log.i("login", jsonObject.getString("errMsg"));
                    final JSONArray result = jsonObject.getJSONArray("result");
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            List<SupBeen> list = SaveOrderInfo.changeSupToList(result.toString());
                            Log.i("list", list.size() + "------");
                            supBeens.clear();
                            supBeens.addAll(list);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    adapter.notifyDataSetChanged();
                                }
                            });
                        }
                    }).start();
                } catch (JSONException e) {

                    e.printStackTrace();
                }
            }
        });

    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return supBeens.size();
        }

        @Override
        public Object getItem(int i) {
            return supBeens.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            Holder holder;
            if (view == null) {
                holder = new Holder();
                view = LayoutInflater.from(SupActivity.this).inflate(R.layout.sup_list_item, null);
                holder.tv = view.findViewById(R.id.sup_item);
                view.setTag(holder);

            } else {
                holder = (Holder) view.getTag();
            }

            holder.tv.setText(supBeens.get(i).getName());

            return view;
        }
        class Holder{
            TextView tv;
        }
    }
}
